package com.example.asus.get_postdemo;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_username;
    private EditText et_password;
    private Button btn_get;
    private Button btn_post;
    private Button btn_getOK;
    private Button btn_postOK;
    private Button btn_uploadOK;

    private ImageView iv;

    private RequestUtil requestUtil = RequestUtil.getInstance();
    private RequestUtilByOKHttp requestUtilByOKHttp = RequestUtilByOKHttp.getInstance();
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btn_get.setOnClickListener(this);
        btn_post.setOnClickListener(this);
        btn_getOK.setOnClickListener(this);
        btn_postOK.setOnClickListener(this);
        btn_uploadOK.setOnClickListener(this);

    }

    private void initView() {
        et_username = (EditText) findViewById(R.id.et_username);
        et_password = (EditText) findViewById(R.id.et_password);
        btn_get = (Button) findViewById(R.id.btn_get);
        btn_post = (Button) findViewById(R.id.btn_post);
        btn_getOK = (Button) findViewById(R.id.btn_getOK);
        btn_postOK = (Button) findViewById(R.id.btn_postOK);
        btn_uploadOK = (Button) findViewById(R.id.btn_uploadOK);
        iv = (ImageView) findViewById(R.id.iv);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_get:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            final String result = requestUtil.Login(RequestUtil.Method.GET,
                                    et_username.getText().toString(), et_password.getText().toString());

                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
                                }
                            });
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            case R.id.btn_post:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            final String result = requestUtil.Login(RequestUtil.Method.POST,
                                    et_username.getText().toString(), et_password.getText().toString());
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
                                }
                            });
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            case R.id.btn_getOK:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            final String result = requestUtilByOKHttp.Login(RequestUtilByOKHttp.Method.GET,
                                    et_username.getText().toString(), et_password.getText().toString(), handler);

                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (result != null) {
                                        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
                                    }

                                }
                            });
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            case R.id.btn_postOK:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            final String result = requestUtilByOKHttp.Login(RequestUtilByOKHttp.Method.POST,
                                    et_username.getText().toString(), et_password.getText().toString(), handler);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
                                }
                            });
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
            case R.id.btn_uploadOK:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            final String result = requestUtilByOKHttp.Login(et_username.getText().toString(),
                                    getApplicationContext(), handler, iv);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
                                }
                            });
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                break;
        }
    }
}
